package vcp.gui.forms;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JOptionPane;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JTextField;
import vcp.gui.components.VCPPanel;
import vcp.gui.forms.validators.DateValidator;
import vcp.gui.forms.validators.EmailValidator;
import vcp.gui.forms.validators.NumericValidator;
import vcp.gui.forms.validators.Time24HoursValidator;
import javax.swing.JLabel;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.JButton;
import control.OrderController;
import control.SimpleClient;
import control.customTimestamp;
import db.parking.beans.Orders;
import java.awt.Color;
import java.sql.Timestamp;
import com.toedter.calendar.JDateChooser;



public class OrderOccasionalForm extends VCPPanel implements ActionListener {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JTextField textCID;
	private JLabel lblNewOrderError;
	private SimpleClient client;
 	
	/* validators */
	private boolean isValidForm=false;
	private JTextField carNo_field;
	private JTextField hTime_field;
	private JTextField email_field;
	private JLabel lblLeaveDate;
	JDateChooser dateChooser;
	private JLabel label;
	private JTextField mTime_field;

	public OrderOccasionalForm(SimpleClient client) {
		  buildForm();
		  this.client = client;
		  
	}
	
	private void buildForm(){

		textCID = new JTextField();
		textCID.setColumns(10);

		JLabel lblCID = new JLabel("Customer ID");

		JButton btnSend = new JButton("create");
		btnSend.addActionListener(this);
		btnSend.setActionCommand("btnSend");

		lblNewOrderError = new JLabel("");
		lblNewOrderError.setForeground(Color.RED);
		
		carNo_field = new JTextField();
		carNo_field.setColumns(10);
		
		hTime_field = new JTextField();
		hTime_field.setColumns(10);
		
		email_field = new JTextField();
		email_field.setColumns(10);
		
		JLabel lblCarNumber = new JLabel("Car Number");
		
		JLabel lblLeaveTime = new JLabel("Leave Time");
		
		JLabel lblEmail = new JLabel("E-Mail");
		
		lblLeaveDate = new JLabel("Leave Date");
		
		dateChooser = new JDateChooser();
		
		label = new JLabel(":");
		
		mTime_field = new JTextField();
		mTime_field.setColumns(10);
		GroupLayout groupLayout = new GroupLayout(this);
		groupLayout.setHorizontalGroup(
			groupLayout.createParallelGroup(Alignment.TRAILING)
				.addGroup(groupLayout.createSequentialGroup()
					.addContainerGap()
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addGroup(groupLayout.createSequentialGroup()
							.addComponent(lblCID)
							.addContainerGap(350, Short.MAX_VALUE))
						.addGroup(groupLayout.createSequentialGroup()
							.addComponent(lblCarNumber, GroupLayout.PREFERRED_SIZE, 79, GroupLayout.PREFERRED_SIZE)
							.addContainerGap(342, Short.MAX_VALUE))
						.addGroup(groupLayout.createSequentialGroup()
							.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
								.addGroup(groupLayout.createSequentialGroup()
									.addComponent(lblLeaveTime, GroupLayout.PREFERRED_SIZE, 79, GroupLayout.PREFERRED_SIZE)
									.addPreferredGap(ComponentPlacement.RELATED)
									.addGroup(groupLayout.createParallelGroup(Alignment.LEADING, false)
										.addComponent(textCID, Alignment.TRAILING)
										.addComponent(carNo_field, Alignment.TRAILING, GroupLayout.DEFAULT_SIZE, 321, Short.MAX_VALUE)
										.addGroup(groupLayout.createSequentialGroup()
											.addComponent(hTime_field, GroupLayout.PREFERRED_SIZE, 29, GroupLayout.PREFERRED_SIZE)
											.addPreferredGap(ComponentPlacement.RELATED)
											.addComponent(label)
											.addPreferredGap(ComponentPlacement.RELATED)
											.addComponent(mTime_field, GroupLayout.PREFERRED_SIZE, 29, GroupLayout.PREFERRED_SIZE))))
								.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
									.addGroup(groupLayout.createSequentialGroup()
										.addComponent(lblEmail, GroupLayout.PREFERRED_SIZE, 79, GroupLayout.PREFERRED_SIZE)
										.addPreferredGap(ComponentPlacement.RELATED)
										.addComponent(email_field, GroupLayout.PREFERRED_SIZE, 321, GroupLayout.PREFERRED_SIZE))
									.addComponent(lblNewOrderError)
									.addGroup(groupLayout.createSequentialGroup()
										.addPreferredGap(ComponentPlacement.RELATED, 208, Short.MAX_VALUE)
										.addComponent(lblLeaveDate, GroupLayout.PREFERRED_SIZE, 79, GroupLayout.PREFERRED_SIZE)
										.addGap(18)
										.addComponent(dateChooser, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
									.addComponent(btnSend, Alignment.TRAILING)))
							.addGap(16))))
		);
		groupLayout.setVerticalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addComponent(lblNewOrderError)
					.addGap(24)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblCID)
						.addComponent(textCID, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(18)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblCarNumber)
						.addComponent(carNo_field, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(18)
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
							.addComponent(lblLeaveTime)
							.addComponent(hTime_field, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
							.addComponent(label)
							.addComponent(mTime_field, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
							.addComponent(lblLeaveDate))
						.addComponent(dateChooser, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(18)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblEmail)
						.addComponent(email_field, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(18)
					.addComponent(btnSend)
					.addContainerGap(40, Short.MAX_VALUE))
		);
		setLayout(groupLayout);

	}

	@Override
	public void actionPerformed(ActionEvent e) {
		
		Orders bean = new Orders();
		OrderController Orders;
		
		//Add input Validators
		NumericValidator verifyID = new NumericValidator();
		Time24HoursValidator verifyTime = new Time24HoursValidator();
		DateValidator verifyDate = new DateValidator();
		EmailValidator verifyEmail = new EmailValidator();
		
		    //Validate that the input is NOT empty
			if(textCID.getText().isEmpty() || carNo_field.getText().isEmpty() || email_field.getText().isEmpty() || hTime_field.getText().isEmpty()
				|| mTime_field.getText().isEmpty())
			{
				JOptionPane.showMessageDialog(null,"Please fill in all fields","Invalid Input Error", JOptionPane.ERROR_MESSAGE);
				isValidForm = false;
				return;
			}
			
		    //Parse all data	
			String ID = textCID.getText();
			String carNo = carNo_field.getText();
			String email = email_field.getText();
			int leaveHours = Integer.parseInt(hTime_field.getText());
			int leaveMinutes = Integer.parseInt(mTime_field.getText());
			String time = hTime_field.getText()+":"+mTime_field.getText(); 
			int day=dateChooser.getJCalendar().getDayChooser().getDay();
			int month=dateChooser.getJCalendar().getMonthChooser().getMonth()+1;
			int year=dateChooser.getJCalendar().getYearChooser().getYear();
			String date = Integer.toString(day)+"/"+ Integer.toString(month) +"/"+ Integer.toString(year);
			String dateFormat = "dd/MM/yyyy";
			
			System.out.println(year+" "+month+" "+day);

			//Input Validity Check
			if (ID.equals("") || carNo.equals("") || !verifyID.validate(ID) || !verifyID.validate(ID)) {
				JOptionPane.showMessageDialog(null,"Invalid ID Entered,Please Re-Enter","Invalid Input Error", JOptionPane.ERROR_MESSAGE);
				isValidForm = false;
			}
			else if (time.equals("") || !verifyTime.validate(time)) {
					JOptionPane.showMessageDialog(null,"Invalid Time Entered,Please Re-Enter","Invalid Time Error", JOptionPane.ERROR_MESSAGE);
					isValidForm = false;
			}
			else if (date.equals("") || !verifyDate.isThisDateValid(date, dateFormat)) {
				JOptionPane.showMessageDialog(null,"Invalid Date Entered,Please Re-Enter","Invalid Date Error", JOptionPane.ERROR_MESSAGE);
				isValidForm = false;
		   }
		   else if (email.equals("") || !verifyEmail.validate(email)) {
				JOptionPane.showMessageDialog(null,"Invalid Email Entered,Please Re-Enter","Invalid Email Error", JOptionPane.ERROR_MESSAGE);
				isValidForm = false;
		   }
		   else{
				isValidForm = true;
			}
			
			if(isValidForm==true){
				 try {
					Orders = new OrderController(client);
					//create new Timestamp for the time the user wants to leave the lot
					Timestamp leaveTime=customTimestamp.makeTimestamp(year,month,day,leaveHours,leaveMinutes,02,02);
					bean.setUid(ID);
					bean.setType("Occasional");
					bean.setCarNo(carNo);
					bean.setLeaveTime(leaveTime);
					bean.setLotNo(0);
					bean.setEmail(email);
					bean.setStatus(0); //TODO:what is the status of order when its been only set?
					//start the Order Controller to insert order into database
					Orders.InsertOcassional(bean);
				} catch (Exception e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
		

		}
			
			
	}
	
}


